package com.teach.service.impl.photo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.teach.dto.CommentsSearch;
import com.teach.dto.LoginUser;
import com.teach.dto.PhotoCommentsDTO;
import com.teach.entity.Comments;
import com.teach.entity.PhotoPic;
import com.teach.entity.UserInfo;
import com.teach.service.CommentsService;
import com.teach.service.UserService;
import com.teach.service.photo.PhotoCommentsService;
import com.teach.service.photo.PhotoPicService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class PhotoCommentsServiceImpl implements PhotoCommentsService {

	@Value("${photoCommentType:摄影}")
	private String photoCommentType;

	@Autowired
	private PhotoPicService photoPicService;

	@Autowired
	private CommentsService commentsService;
	
	@Autowired
	private CommentsSearch search;

	@Autowired
	private UserService userService;

	
	@Override
	public Object commentInform(LoginUser user) {
		 List<PhotoPic> photoPics = photoPicService.selectByWriterId(user.getUserName());
		//用户评论dto集合
		List<PhotoCommentsDTO> commentLlist = new ArrayList<>();
		//遍历photo作品，依据photo作品id 查出本模块的评论
		for (PhotoPic photoPic : photoPics) {
			search.setCommentCommentator(photoPic.getPhotoPicId());
			log.debug("search : {}", search);
			PageInfo<Comments> commentsPage = commentsService.commentsPage(search);
			for (Comments comments : commentsPage.getList()) {
				if(!photoCommentType.equals(comments.getCommentType())) {
					continue;
				}
				//将评论id ,评论时间，内容放入dto
				PhotoCommentsDTO commentsDTO = new PhotoCommentsDTO(comments.getCommentContent());
				commentsDTO.setPhotoProductId(comments.getCommentId());
				commentsDTO.setUserName(comments.getCommentUserName());
				commentsDTO.setPhotoTriggerTime(comments.getCommentTime());
				commentLlist.add(commentsDTO);
			}
		}
		log.debug("commentLlist: {}", commentLlist);
		for (PhotoCommentsDTO pcdto : commentLlist) {
		//	log.info("pcdto ===============> {}", pcdto.getUserName());
			UserInfo userInfo = userService.get(pcdto.getUserName());
			pcdto.setUserImg(userInfo.getUserImg());
		}
		log.info("commentLlist ==> {}", commentLlist);
		return commentLlist;
	}

}
